﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Configuration;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Article;
using DataObject.Category;
using DataObject.Online;

public partial class Controls_Common_TopNavigator : System.Web.UI.UserControl
{
    protected string IsLogined = "none";
    protected string IsNoLogin = "block";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (HttpContext.Current.Session["e3group.denhongphuc.customer"] != null)
        {
            ltCustomer.Text = string.Format("<a class=\"customer-icon\" href=\"{0}\">Xin chào, {1}</a>", UrlHelpers.CustomerAccount, HttpContext.Current.User.Identity.Name);
            IsLogined = "block";
            IsNoLogin = "none";
        }

        if (!IsPostBack)
        {
            ListOnline();

            ListCategory();

            rptPayment.DataSource = GetListByCategoryIdParent(ConvertUtility.ToInt32(WebConfigurationManager.AppSettings["StaticPayment"]));
            rptPayment.DataBind();

            rptGuide.DataSource = GetListByCategoryIdParent(ConvertUtility.ToInt32(WebConfigurationManager.AppSettings["StaticGuide"]));
            rptGuide.DataBind();
        }
    }

    private void ListOnline()
    {
        rptOnline.DataSource = OnlineController.Online_GetByTypeId((int)OnlineType.Top);
        rptOnline.DataBind();
    }

    private void ListCategory()
    {
        var categories = CategoryController.Category_GetByParentId((int)CategoryType.Product, Constants.PARENT_ROOT, Constants.ACTIVE);
        if (categories.Count > 0)
        {
            rptCategory.DataSource = categories;
            rptCategory.DataBind();
        }
    }

    private List<ArticleObject> GetListByCategoryIdParent(int categoryId)
    {
        return ArticleController.Article_GetListByCategoryId_Parent(categoryId);
    }

    protected void rptPayment_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            var ltTitle = (Literal)e.Item.FindControl("ltTitle");
            var ltImage = (Literal)e.Item.FindControl("ltImage");

            var categoryId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfCategoryId")).Value);
            var articleId  = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfArticleId")).Value);
            var title      = ((HiddenField)e.Item.FindControl("hfTitle")).Value;

            var categoryName = CategoryController.GetCategoryName(categoryId);

            var imgHref  = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size=85", Constants.ThumbImage, articleId, (int)CategoryType.Article);
            var linkHref = UrlHelpers.ArticleStatic((int) StaticType.Category, categoryId, title);
            
            var articleHref     = string.Format("<a href=\"{0}\">{1}</a>", linkHref, categoryName);
            var articleImgHref = string.Format("<a href=\"{0}\"><img src=\"{1}\" alt=\"\"/></a>", linkHref, imgHref);

            ltTitle.Text = articleHref;
            ltImage.Text = articleImgHref;            
        }
    }
}